package com.cty.mapper;

import com.cty.pojo.Student;
import com.cty.pojo.Teacher;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Auther: cty
 * @Date: 2020/3/1 22:40
 * @Description:  MyBatis注解
 * @version: 1.0
 */
public interface StudentMapper {
    // 1 MyBatis注解实现查询
    @Select("select * from student")
    List<Student> selAll();

    // 2 MyBatis注解实现新增
    @Insert("insert into student values(#{sno}, #{sname}, #{tno})")
    int insStudent(Student student);

    // 3 MyBatis注解实现修改
    @Update("update student set tno=#{tno} where sno=#{sno}")
    int updStudent(Student student);

    // 4 MyBatis注解实现删除
    @Delete("delete from student where sno=#{sno}")
    int delStudent(Student student);

    // 5 MyBatis注解实现<resultMap>功能：关联单个对象（N+1方式）
    @Results(value = {
            @Result(id = true, column = "sno", property = "sno"),
            @Result(column = "sname", property = "sname"),
            @Result(column = "tno", property = "tno"),
            @Result(column = "tno", property = "teacher", one = @One(
                    select = "com.cty.mapper.TeacherMapper.selByTno"
            ))
    })
    @Select("select * from student")
    List<Student> selStudent();

//    // 6 MyBatis注解实现<resultMap>功能：关联单个对象（联合查询方式）——如何实现？
//    @Results(value = {
//            @Result(id = true, column = "s_sno", property = "sno"),
//            @Result(column = "s_sname", property = "sname"),
//            @Result(column = "s_tno", property = "tno"),
//            @Result(property = "teacher", javaType = "Teacher", one = @One(
//                    @Result(column = "t_tno", property = "tno"),  //这里出错！
//                    @Result()
//            ))
//    })
//    @Select("select * from student")
//    List<Student> selStudent2();

    // 7 MyBatis注解实现<resultMap>功能：关联集合对象（N+1方式）  见TeacherMapper
    @Select("select * from student where tno=#{0}")
    List<Student> selByTno(int tno);

    // 8 MyBatis注解实现<resultMap>功能：关联集合对象（联合查询方式）——如何实现？

    // 9 创建数据表
    @Update("create table if not exists test(uid int primary key auto_increment, uname varchar(100))")
    int createTable();

    // 10 修改数据表
    @Update("alter table test add pwd varchar(100) after uname")
    int alterTable();

    // 11 删除数据表
    @Update("drop table test")
    int dropTable();




}
